package Controller;

import Service.PromotionAdService;
import com.github.pagehelper.PageInfo;
import domain.PromotionAd;
import domain.PromotionAdVO;
import domain.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    /*
          广告分页查询
     */
    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAdByPage(PromotionAdVO promotionAdVO){

        PageInfo<PromotionAd> pageInfo = promotionAdService.findPromotionAdByPage(promotionAdVO);

        ResponseResult responseResult = new ResponseResult(true,200,"分页查询成功",pageInfo);
        return  responseResult;
    }

    /*
            图片上传代码
     */
    @RequestMapping("/PromotionAdUpload")
     public ResponseResult fileUpload(@RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {

         //1. 判断上传文件是否为空
         if(file.isEmpty()){
             throw new RuntimeException();
         }


         //2. 获取项目部署路径
         String realPath = request.getServletContext().getRealPath("/");

         String path = realPath.substring(0, realPath.indexOf("ssm_web"));

         //3.获取原文件名字
         String filename = file.getOriginalFilename();

         //4. 生成 新文件名
         String newname = System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));

         //5. 文件上传
         String newPath = path + "upload";
         File fileDoc = new File(newPath, newname);

         // 如果不存在就新建目录
         if(!fileDoc.getParentFile().exists()){
             fileDoc.getParentFile().mkdirs();
             System.out.println("newFilePath" + fileDoc);
         }

         //上传
         file.transferTo(fileDoc);

         //6. 返回 文件名 和 文件路径
         HashMap<String, String> map = new HashMap<>();

         map.put("filename",newname);
         map.put("filepath","http://localhost:8080/upload/"+newname);

         ResponseResult responseResult = new ResponseResult(true, 200, "图片上传成功", map);
         return responseResult;

     }

     /*
            动态更改广告上下线状态
      */
    @RequestMapping("/updatePromotionAdStatus")
     public ResponseResult updatePromotionAdStatus(int id, int status){

        promotionAdService.updatePromotionAdStatus(id,status);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("status",status);

        ResponseResult responseResult = new ResponseResult(true, 200, "状态修改成功", map);

        return responseResult;

    }
}
